package com.gallery.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gallery.result.Result;
import com.gallery.service.IImagesService;
import com.gallery.service.ILoveImagesService;
import com.gallery.service.LogImagesService;
import dto.PageDTO;
import entity.Images;
import entity.LogImages;
import entity.LoveImages;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import query.ImagesQuery;
import vo.ImagesVO;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @return
 */
@RestController
@RequestMapping("/images")
@RequiredArgsConstructor
@Slf4j
public class ImagesController {
    private final IImagesService imagesService;

    private final ILoveImagesService iLoveImagesService;

    private final LogImagesService logImagesService;

    private final RedisTemplate redisTemplate;


    /**
     * 分页查询个人作品
     * @param imagesQuery
     * @return
     */
    @GetMapping("/page")
    public Result<PageDTO<ImagesVO>> page(ImagesQuery imagesQuery){
        //查询redis是否有照片
        //有的话就直接返回图片
        String key = "user_"+ StpUtil.getLoginIdAsLong();
        List<Images> images=new ArrayList<>();
        images = (List<Images>) redisTemplate.opsForValue().get(key);
        if (images == null ||images.size() <= 0) {
            //没有则先查询再存入redis
            images = imagesService.getall(StpUtil.getLoginIdAsLong());
            redisTemplate.opsForValue().set(key,images);
        }
        //分页处理对结果
        PageDTO<ImagesVO> page=imagesService.getImageQuery(images,imagesQuery);
        //return Result.success(imagesService.querypage(imagesQuery));
        return Result.success(page);
    }

    /**
     * 浏览图片
     * @param logImages
     * @return
     */
    @PostMapping("/log")
    public Result loveImages(LogImages logImages){
        Long userId = StpUtil.getLoginIdAsLong();
        //存入浏览表
        logImages.setUserId(userId);
        logImages.setCreateTime(LocalDateTime.now());

        logImagesService.save(logImages);
        //总活加1
        imagesService.upHot(logImages.getImagesId());
        redisTemplate.delete("user_"+ StpUtil.getLoginIdAsLong());
        return Result.success();
    }

    /**
     * 根据照片id修改status
     * @param images
     * @return
     */
    @PostMapping("/status")
    public Result status(Images images){
        String status = images.getStatus();
        Long id = images.getId();
        LambdaUpdateWrapper<Images> wrapper = new LambdaUpdateWrapper<Images>()
                .set(Images::getStatus, status)
                .eq(Images::getId, id);
        imagesService.update(null,wrapper);
        redisTemplate.delete("user_"+ StpUtil.getLoginIdAsLong());
        return Result.success();
    }


    /**
     * 根据照片id查询日浏览
     * @param images
     * @param localDate
     * @return
     */
    @GetMapping("/dayhot")
    public Result<ImagesVO> dayhot(Images images, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate){
        LocalDateTime minlocalDateTime = localDate.atTime(0,0,0);
        LocalDateTime maxlocalDateTime = localDate.atTime(23,59,59);
        LambdaQueryWrapper<LogImages> wrapper = new LambdaQueryWrapper<LogImages>()
                .ge(LogImages::getCreateTime, minlocalDateTime)
                .le(LogImages::getCreateTime, maxlocalDateTime)
                .eq(LogImages::getImagesId, images.getId());
        int count = logImagesService.count(wrapper);
        ImagesVO imagesVO = new ImagesVO();
        imagesVO.setDayHot((long) count);
        return Result.success(imagesVO);
    }


    /**
     * 图片喜欢
     * @param imagesId
     * @return
     */
    @PostMapping("/vote")
    public Result vote(@RequestParam Long imagesId ){
        LoveImages loveImages = new LoveImages();
        loveImages.setImagesId(imagesId);
        loveImages.setUserId(StpUtil.getLoginIdAsLong());
        loveImages.setCreateTime(LocalDateTime.now());
        iLoveImagesService.save(loveImages);
        redisTemplate.delete("user_"+ StpUtil.getLoginIdAsLong());
        return Result.success();
    }

    /**
     * 取消图片喜欢
     * @param imagesId
     * @return
     */
    @PostMapping("/novote")
    public Result novote(@RequestParam Long imagesId ){
        LambdaQueryWrapper<LoveImages> wrapper = new LambdaQueryWrapper<LoveImages>()
                .eq(LoveImages::getImagesId, imagesId)
                .eq(LoveImages::getUserId, StpUtil.getLoginIdAsLong());
        iLoveImagesService.remove(wrapper);
        redisTemplate.delete("user_"+ StpUtil.getLoginIdAsLong());
        return Result.success();
    }

    /**
     * 批量删除图片
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Long>ids){

        imagesService.deleteBatch(ids);
        redisTemplate.delete("user_"+ StpUtil.getLoginIdAsLong());
        return Result.success();
    }
    /**
     * 一次性随机获取十张图用于大厅主页面展示
     * 若登录则调用下面那个接口
     * @return
     */
    @GetMapping("/random")
    public Result<List<ImagesVO>>randomImages(){
        List<ImagesVO> imagesVOS=imagesService.getRandoms();
        return Result.success(imagesVOS);
    }

    /**
     * 为用户推荐十张图片
     * @return
     */
    @GetMapping("/selfrandom")
    public Result<List<ImagesVO>>randomImagesForUser(){
        Long userId = StpUtil.getLoginIdAsLong();
        return Result.success(imagesService.getRandomsByUser(userId));
    }

    /**
     * 获取十条最热clazz用于前端展示
     * @return
     */
    @GetMapping("/clazz")
    public Result<List<String >>getClazz(){

        return Result.success(imagesService.getHotClazz());
    }

    /**
     * 根据id查询照片详情
     * @param id
     * @return
     */
    @GetMapping("/id")
    public Result<ImagesVO> detailed(Long id){
        ImagesVO imagesVO = imagesService.detailed(id);
        return Result.success(imagesVO);
    }

}
